--- Request Parser module
--- @author chengyu liu mliuchengyu@live.cn
local cjson = require "cjson"

local args = {}
local Request = {}
local request_method = ngx.var.request_method
local receive_headers = ngx.req.get_headers()
local content_length = tonumber(receive_headers['content-length'])

-- 字符串处理函数
local function explode ( _str,seperator )
        local pos, arr = 0, {}
            for st, sp in function() return string.find( _str, seperator, pos, true ) end do
                    table.insert( arr, string.sub( _str, pos, st-1 ) )
                    pos = sp + 1
            end
        table.insert( arr, string.sub( _str, pos ) )
        return arr
end

if "GET" == request_method and content_length > 0 then
        args = ngx.req.get_uri_args()
elseif "POST" == request_method and content_length > 0 then
        ngx.req.read_body()
        -- media type: multipart/form-data
        if string.sub(receive_headers["content-type"],1,20) == "multipart/form-data;" then
                local receive_headers = ngx.req.get_headers()
                content_type = receive_headers["content-type"]
                body_data = ngx.req.get_body_data()
                local new_body_data = {}
                local boundary = "--" .. string.sub(receive_headers["content-type"],31)
                local body_data_table = explode(tostring(body_data),boundary)
                local first_string = table.remove(body_data_table,1)
                local last_string = table.remove(body_data_table)

                for i,v in ipairs(body_data_table) do
                    local t = explode(v,"\r\n\r\n")
                    local key = string.sub(t[1],41,-2)
                    local value = string.sub(t[2],1,-3)
                    args[key] = value
                end
        elseif string.sub(receive_headers["content-type"],1,20) == "application/json" then
                -- media type: application/json
                args = cjson.decode(ngx.req.get_body_data())
        else
                -- media type: application/x-www-form-urlencoded
                args = ngx.req.get_post_args()
        end
end

-- 当前的请求类型
-- @return string
function Request:method()
        return ngx.var.request_method
end

-- 是否为GET请求
-- @return bool
function Request:isGet()
        return self:method() == "GET"
end


-- 是否为POST请求
-- @return bool
function Request:isPost()
        return self:method() == "POST"
end


-- 获取POST参数
-- @param  string   name header名称
-- @return string|array
function Request:post(name)
        if name == '' or name == nil then
                return args
        else
                return args[name]
        end
end

-- 获取GET参数
-- @param  string   name header名称
-- @return string|array
function Request:get(name)
        if name == '' or name == nil then
                return args
        else
                return args[name]
        end
end

-- 获取当前的Header
-- @param  string   name header名称
-- @return string|array
function Request:header(name)
        if name == '' or name == nil then
                return receive_headers
        else
                return receive_headers[name]
        end
end

return Request